<?php

namespace backend\models;

use Yii;
use yii\helpers\ArrayHelper;
use mdm\admin\models\User;

/**
 * 管理员表
 * 后台的 yii::$app->user 对应的不是这个类，但跟这个类操作的是同一个数据库表
 *
 * @property int $id
 * @property string $username
 * @property string $auth_key
 * @property string $password_hash
 * @property string|null $password_reset_token
 * @property string $email
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 */
class Admin extends User
{

    public static $statusConf = [
        self::STATUS_ACTIVE => '正常',
        self::STATUS_INACTIVE => '禁用',
    ];

    /**
     * 大多记录中保存的是admin的uid，此方法用于列表中展示admin的用户名
     * @return array
     */
    public static function getAuditorMap(){
        static $data;
        if(!$data){
            $res = Admin::find()->asArray()->select('id,username')->all();
            if($res) $data = ArrayHelper::map($res, 'id', 'username');
        }
        return $data;
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'admin';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at'], 'required'],
            [['status', 'created_at', 'updated_at'], 'integer'],
            [['username', 'auth_key'], 'string', 'max' => 32],
            [['password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
            ['status', 'default', 'value' => self::STATUS_INACTIVE],
            ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_INACTIVE]],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => '用户名',
            'auth_key' => 'Auth Key',
            'password_hash' => '密码',
            'password_reset_token' => 'Password Reset Token',
            'email' => 'Email',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
        ];
    }
}
